package com.github.glomadrian.materialanimatedswitch.utils;

import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * @author Adrián García Lomas
 */
public class Utils {
    /**
     * 得到设备的密度
     *
     * @param context ctx
     * @return float
     */
    public static float getScreenDensity(Context context) {
        DisplayAttributes attributes = DisplayManager
                .getInstance()
                .getDefaultDisplay(context)
                .get()
                .getRealAttributes();
        return attributes.densityPixels;
    }

    /**
     * Convert vp to Pixel
     */
    /**
     * 把密度转换为像素
     *
     * @param vpValue dp值
     * @return int
     */
    public static int vp2px(Context context, float vpValue) {
        float scale = getScreenDensity(context);
        return (int) (vpValue * scale + 0.5);
    }
}
